/*
  LinPVR - Linux Personal Video Recorder
  Copyright (C) 2006 Kamil Pawlowski <kamilpe@gmail.com>

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
#ifndef _DEBUG_H_
#define _DEBUG_H_


/*
  Here we are three macros. DEBUG_RETVAL is a wrapper for return functions.
  If function return error, DEBUG_RETVAL put informations in to messages pool,
  but himself return the same value as function wich wrapped. So application
  can catch return value and report log in pool in the same time. If return
  value is < 0 then debug_message() return trace and error messages. 
  DEBUG_RETVAL2 is the same but have second parametr. This parametr should be 
  string for error message. Example ERR_OPEN can recieve param for what file
  can't be opened. 
  DEBUG_ENTER recieve function name, this is using for backtrace.
  If VERBOSE is defined messages pool has being real time show on stdout.
 */
#define VERBOSE
#define DEBUG_RETVAL(x)    debug_error(x,__LINE__,__FILE__)
#define DEBUG_RETVAL2(x,y) debug_error2(x,y,__LINE__,__FILE__)
#define DEBUG_ENTER(x)     debug_enter(x,__LINE__,__FILE__)

#define EVENTS_IN_MEMORY      34

#define ERR_OPEN              -1
#define ERR_NO_V4L2           -2
#define ERR_NO_CAPTURE        -4
#define ERR_FORMAT            -5
#define ERR_BUFFERS_REQUEST   -6
#define ERR_BUFFERS_FEW       -7
#define ERR_MMAP              -8
#define ERR_STREAMON          -9
#define ERR_MALLOC            -10
#define ERR_THEORA            -11
#define ERR_VORBIS            -12
#define ERR_MUNMAP            -14
#define ERR_CLOSE             -15
#define ERR_BUFFERS_QUERY     -16
#define ERR_QBUF              -17
#define ERR_DQBUF             -18
#define ERR_STREAMOFF         -19
#define ERR_NO_DEVICE         -20
#define ERR_UNDEFINED         -21
#define ERR_NO_DATA           -22
#define ERR_CONFIG            -23
#define ERR_REGCOMP           -24
#define ERR_GUI               -25
#define ERR_AUDIO             -26
#define TYPE_ERROR            1
#define TYPE_FUNC_TRACE       2

typedef struct event_s {
  int type; 
  int code;  /* code from functions return */
  char desc[1024];
  int line;
  const char *file;
} event_t;

typedef struct debug_s {
  event_t events[EVENTS_IN_MEMORY+1];
  int queue;
} debug_t;

void         debug_init();
void         debug_flush();
void         debug_enter(const char *funcname, int line, const char *file);
int          debug_error(int code, int line, const char *file);
int          debug_error2(int code, const char *param, int line, const char *file);
const char * debug_message();
event_t *    new_event();

#endif 
